package com.xohm.platform.api;

import java.io.IOException;
import java.io.*;
import java.util.*;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class DriverXMLParserHandler extends DefaultHandler
{
	private String elementValue  = null;
	private DriverInformation driverInfo = null;
	private int entry            = 0;
	private Vector<DriverInformation> data = new Vector<DriverInformation>();
	
	// Constants
	public static final String PLIST  = "plist";
	public static final String DICT   = "dict";
	public static final String KEY    = "key";
	public static final String ARRAY  = "array";
	public static final String STRING = "string";

	public DriverXMLParserHandler()
	{
	}
	
	public static Vector<DriverInformation> parsePropertiesXML(InputSource xmlInputSource)
	{
		// Instance of this class is the handler.
		DriverXMLParserHandler handler = new DriverXMLParserHandler();

		//get a factory
		SAXParserFactory spf = SAXParserFactory.newInstance();
		try
		{
			//get a new instance of parser
			spf.setValidating(false);
			SAXParser sp = spf.newSAXParser();
			
			// Get stripped down XML
			InputSource xmlToParse = stripXMLDoc(xmlInputSource);

			// parse the file and also register this class for call backs
			sp.parse(xmlToParse, handler);
		
		}catch(SAXException se) {
			se.printStackTrace();
		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
		}catch (IOException ie) {
			ie.printStackTrace();
		}
		
		return handler.data;
	}
	
	private static InputSource stripXMLDoc(InputSource xmlInputSource) throws IOException
	{
		InputStreamReader reader = new InputStreamReader(xmlInputSource.getByteStream());
		String xmlString = "";
		int charValue = 0;
		while ((charValue = reader.read()) != -1) {
			xmlString = xmlString + (char) charValue;
		}
		int startPos = xmlString.indexOf("DOCTYPE");
		if (startPos > -1)
		{
			startPos = xmlString.indexOf(">", startPos);
			xmlString = xmlString.substring(startPos+1);
		}
		reader.close();
		
		return new InputSource(new java.io.StringReader(xmlString));
	}
	
	public void startDocument() throws SAXException
	{
		// Nothing to do here
	}
	
	public void startElement(String uri, String localName, String qName, Attributes atts)
		throws SAXException
	{
		if (qName.equalsIgnoreCase(KEY))
		{
			driverInfo = new DriverInformation();
			entry = 0;
		}
	}
	
	public void endElement(String uri, String localName, String qName) throws SAXException
	{
		if (qName.equalsIgnoreCase(KEY))
		{
			driverInfo.setVendorName(elementValue);
			data.add(driverInfo);
		}
		else
		if (qName.equalsIgnoreCase(STRING))
		{
			switch (entry)
			{
				case 0:
					driverInfo.setDriverName(elementValue);
					break;
				case 1:
					driverInfo.setDriverLocation(elementValue);
					break;
				case 2:
					driverInfo.setDriverVersion(elementValue);
					break;
				case 3:
					driverInfo.setInstallationDate(elementValue);
					break;
			}
			entry = entry + 1;
		}
		elementValue = null;
	}
	
	/**
	 * End of XML document - Nothing to do. 
	 * 
	 * @throws org.xml.sax.SAXException
	 */
	public void endDocument() throws SAXException
	{
		// Nothing to do here
	}
	
	public void characters(char ch[], int start, int length) throws SAXException
	{
		if (elementValue == null)
			elementValue = new String(ch,start,length).trim();
		else
			elementValue = elementValue + new String(ch,start,length).trim();
	}
}
